<template>
    <div class="goodsList_wrap">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片区域 -->
        <el-card>
            <el-row :gutter="20">
                <!-- 搜索区域 -->
                <el-col :span="6">
                    <el-input @clear="showAllGoods" clearable placeholder="请输入内容" v-model="searchText" class="input-with-select">
                        <el-button @click="searchGoods" slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <!-- 添加商品按钮 -->
                <el-col :span="4">
                    <el-button @click="addGoods" type="primary">添加商品</el-button>
                </el-col>
            </el-row>

            <!-- 商品列表展示区 -->
            <el-table :data="goodsList" border style="width: 100%">
                <el-table-column type="index"></el-table-column>
                <el-table-column prop="goods_name" label="商品名称" width="400"></el-table-column>
                <el-table-column prop="goods_price" label="商品价格（元）"></el-table-column>
                <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
                <el-table-column label="创建时间">
                    <template slot-scope="scope">
                        {{scope.row.add_time | setTimer}}
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="small" type="primary" icon="el-icon-edit"></el-button>
                        <el-button @click="removeGoods(scope.row.goods_id)" size="small" type="danger" icon="el-icon-delete"></el-button>
                    </template> 
                </el-table-column>
            </el-table>

            <!-- 分页区域 -->
             <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="goodsListParams.pagenum"
            :page-sizes="[5, 10, 15, 20]" :page-size="goodsListParams.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>

        </el-card>

        

    </div>
</template>

<script>
export default {
    data() {
        return {
            // 搜索文本
            searchText: '',
            // 商品列表
            goodsList: [],
            // 商品查询参数
            goodsListParams: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            //商品数量
            total: 0,
        }
    },
    created() {
        this.getGoodsList();
    },
    methods: {
        // 获取商品列表
        async getGoodsList() {
            const { data: res } = await this.$http.get('goods', { params: this.goodsListParams });
            if (res.meta.status != 200) return this.$message.error('获取商品列表失败，请刷新重试');
            this.goodsList = res.data.goods;
            this.total = res.data.total;
        },
        // 获取改变后的显示总条数
        handleSizeChange(newSize) {
            this.goodsListParams.pagesize = newSize;
            this.getGoodsList();
        },
        // 获取改变后的当前页码
        handleCurrentChange(newNum) {
            this.goodsListParams.pagenum = newNum;
            this.getGoodsList();
        },
        // 搜索商品
        searchGoods() {
            this.goodsListParams.query = this.searchText;
            this.getGoodsList();
        },
        // 清空搜索文本，显示全部商品
        showAllGoods() {
            this.searchGoods();
        },
        // 删除商品
        removeGoods(goodsID) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const { data: res } = await this.$http.delete(`goods/${goodsID}`);
                if (res.meta.status != 200) return this.$message.error('删除商品失败');
                this.$message.success('删除商品成功');
                this.getGoodsList();
            }).catch(() => {
                this.$message.info('取消了删除操作');
            });
        },
        // 前往添加商品页面
        addGoods() {
            this.$router.push('/goods/addGoods');
        }
    }
}
</script>

<style lang="less" scoped>
.el-table {
    font-size: 12px;
}
.picHeight {
    overflow: hidden;
    display: auto;
    height: 300px;
}
</style>